package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.entity.Address;
import com.qf.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/18 19:51
 * @Version 1.0
 */
public class AddressDaoImpl implements AddressDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public List<Address> selectAddressesByUid(Integer uid) {
        String sql="select*from tb_address where uid=?";
        List<Address> query=null;
        try {
            query=qr.query(DbUtils.getConnection(),sql,new BeanListHandler<>(Address.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void updateLevelByUid(int uid) {
        String sql ="update tb_address set level=1 where uid=?";
        try {
            qr.update(DbUtils.getConnection(),sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int updateLevelById(int id) {
        int result=0;
        String sql="update tb_address set level=0 where id=?";
        try {
            result=qr.update(DbUtils.getConnection(),sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Address selectAddressesById(Integer aid) {
        String sql="select * from tb_address where id=?";
        Address query=null;
        try {
            query = qr.query(DbUtils.getConnection(), sql, new BeanHandler<>(Address.class), aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
